package shutdowner;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;


public class MainWindow {

	private static Display display;
	private static Spinner min_spinner;
	private static Spinner sec_spinner;
	private static Shell shlShutdown;
	private static Label lblMin;
	private static Label lblSek;

	public static void displayNewTimeOut(final int timeout) {
		if (!(display == null) && !display.isDisposed()) {
			display.asyncExec(new Runnable() {

				@Override
				public void run() {
					min_spinner.setSelection(timeout / 60);
					sec_spinner.setSelection(timeout % 60);
				}
			});
		}
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		display = Display.getDefault();
		shlShutdown = new Shell();
		shlShutdown.setSize(472, 187);
		shlShutdown.setText("Shutdown");

		final ShutdownThread shutdownThread = new ShutdownThread(10);
		shutdownThread.start();

		final Button btnStartPause = new Button(shlShutdown, SWT.NONE);
		// Start Stop Countdown in Background Thread
		btnStartPause.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (shutdownThread.isRunning()) {
					shutdownThread.stopCountdown();
					btnStartPause.setText("Start");
				} else {
					shutdownThread.startCountdown();
					btnStartPause.setText("Stop");
				}
			}
		});
		btnStartPause.setBounds(23, 81, 360, 38);
		btnStartPause.setText("Start");

		Label lblSystemWirdHeruntergefahren = new Label(shlShutdown, SWT.NONE);
		lblSystemWirdHeruntergefahren.setBounds(23, 39, 230, 20);
		lblSystemWirdHeruntergefahren
				.setText("System wird heruntergefahren in:");

		min_spinner = new Spinner(shlShutdown, SWT.BORDER);
		min_spinner.setToolTipText("Minuten");
		min_spinner.setMaximum(999);
		min_spinner.setSelection(1);
		min_spinner.setBounds(259, 36, 59, 26);
		
		// set new Timeout on Modify of either spinner
		// also set Value to Spinner in case it was changed by counting down..
		min_spinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				int newTimeout = 0;
				try {
					newTimeout = min_spinner.getSelection() * 60
							+ sec_spinner.getSelection();
					shutdownThread.setNewTimeout(newTimeout);
				} catch (NumberFormatException ex) {
					JOptionPane
							.showMessageDialog(null,
									"Input value is not a number. Please use integers only.");
				}
			}
		});

		sec_spinner = new Spinner(shlShutdown, SWT.BORDER);
		sec_spinner.setToolTipText("Sekunden");
		sec_spinner.setMaximum(59);
		sec_spinner.setBounds(356, 36, 59, 26);
		
		lblMin = new Label(shlShutdown, SWT.NONE);
		lblMin.setText("min");
		lblMin.setBounds(324, 39, 28, 20);
		
		lblSek = new Label(shlShutdown, SWT.NONE);
		lblSek.setText("sek");
		lblSek.setBounds(421, 39, 28, 20);
		
		// set new Timeout on Modify of either spinner
		// also set Value to Spinner in case it was changed by counting down..
		sec_spinner.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				int newTimeout = 0;
				try {
					newTimeout = min_spinner.getSelection() * 60
							+ sec_spinner.getSelection();
					shutdownThread.setNewTimeout(newTimeout);
				} catch (NumberFormatException ex) {
					JOptionPane
							.showMessageDialog(null,
									"Input value is not a number. Please use integers only.");
				}
			}
		});

		shlShutdown.open();
		shlShutdown.layout();
		while (!shlShutdown.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		MainWindow.closeAll();
	}

	public static void closeAll() {
		display.syncExec(new Runnable() {
			
			@Override
			public void run() {

				shlShutdown.dispose();
				display.dispose();
				System.exit(0);
			}
		});
	}

}
